package org.jetlinks.community.mstn.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.crud.service.ReactiveCrudService;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.jetlinks.community.mstn.entity.CustomFormEntity;
import org.jetlinks.community.mstn.entity.CustomFormLogEntity;
import org.jetlinks.community.mstn.service.CustomFormService;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * 自定义表单
 *
 */
@RestController
@RequestMapping("/form")
@Authorize
@Resource(id = "form", name = "自定义表单", group = "mstn")
@Tag(name = "自定义表单")
@AllArgsConstructor
public class CustomFormController implements ReactiveServiceCrudController<CustomFormEntity, String> {

    private final CustomFormService customFormService;

    @Override
    public ReactiveCrudService<CustomFormEntity, String> getService() {
        return customFormService;
    }

    /**
     * 上报数据
     * @param customFormLogEntity
     * @return
     */
    @PostMapping("/upload/data/{formId}")
    @Authorize(merge = false)
    @Operation(summary = "上报数据")
    public Mono<Void> uploadData(@PathVariable String formId,
                                 @RequestBody CustomFormLogEntity customFormLogEntity){
        return customFormService.saveUploadData(formId,customFormLogEntity);
    }

    @GetMapping("/query/data/{formId}")
    @Authorize(merge = false)
    @Operation(summary = "查询数据")
    public Mono<PagerResult<CustomFormLogEntity>> queryData(@PathVariable String formId,
                                                            @Parameter QueryParamEntity query) {
        return customFormService.queryUploadData(formId, query);
    }
    @GetMapping("/query/data/{formId}/{logId}")
    @Authorize(merge = false)
    @Operation(summary = "查询数据")
    public Mono<CustomFormLogEntity> queryData(@PathVariable String formId,
                                               @PathVariable String logId) {
        return customFormService.queryUploadDataById(formId, logId);
    }

    @DeleteMapping("/del/data/{formId}/{logId}")
    @Authorize(merge = false)
    @Operation(summary = "删除数据")
    public Mono<Long> deleteData(@PathVariable String formId,
                                 @PathVariable String logId) {
        return customFormService.deleteUploadDataById(formId, logId);
    }
    @PutMapping("/update/data/{formId}")
    @Authorize(merge = false)
    @Operation(summary = "修改数据")
    public Mono<Void> updateData(@PathVariable String formId,@RequestBody CustomFormLogEntity customFormLogEntity) {
        return customFormService.updateUploadData(formId, customFormLogEntity);
    }

}
